from collections import Counter
import re

# 读取《Hamlet》文本文件
with open('./素材/hamlet.txt', 'r', encoding='utf-8') as file:
    text = file.read()

# 使用正则表达式匹配单词
words = re.findall(r'\b\w+\b', text.lower())

# 统计词频
word_freq = Counter(words)

# 输出词频最高的20个词
top_words = word_freq.most_common(20)

print("《Hamlet》中词频最高的20个词：")
print("{:<15}{}".format("单词", "次数"))
for word, count in top_words:
    print("{:<15}{}".format(word, count))
